import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/list/refresh.dart';
import 'package:industry_net/base/component/list/section.dart';
import 'package:industry_net/pages/home/main/views/header.dart';
import 'package:industry_net/pages/news/main/controller.dart';
import 'package:industry_net/pages/news/main/models/news.dart';
import 'package:industry_net/pages/news/main/views/content/widgets/images_cell.dart';
import 'package:industry_net/pages/news/main/views/content/widgets/text_cell.dart';
import 'state.dart';
import 'widgets/video_cell.dart';
import 'widgets/cell.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ContentController extends GetxController {
  final parentCtrl = Get.find<NewsController>();
  final parentState = Get.find<NewsController>().state;
  final state = ContentState();

  @override
  void onInit() {
    super.onInit();
    getData();
    observer();
  }

  void observer() {}

  void getData() {
    var _items = List.generate(
            30,
            (index) => NewsModel()
              ..type =
                  NewsType.values[Random().nextInt(NewsType.values.length)])
        .toList();
    state.dataSource = [
      SectionModel(
          index: 0,
          itemBuilder: (context, index) {
            var md = _items[index];
            switch (md.type) {
              case NewsType.video:
                return VideoCell(md: md);
              case NewsType.rich:
                return NewsCell(
                  md: md,
                );
              case NewsType.tiro:
                return InkWell(
                  onTap: () => actionCellClick(md),
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                        vertical: gap10, horizontal: gap12),
                    child: TripView(),
                  ),
                );
              case NewsType.images:
                return NewsImagesCell(
                  md: md,
                );
              default:
                return NewsTextCell(
                  md: md,
                );
            }
          },
          items: _items)
    ];
  }

  void actionCellClick(NewsModel md) {
    Get.toNamed(RouterPath.news_detail, arguments: md);
  }

  void onRefresh() async {
    state.pageIndex = 1;
    await Future.delayed(const Duration(seconds: 3));
    RefreshHeaderFooter.stop(state.effect.refreshCtrl, isSuccess: true);
  }

  void onLoading() async {
    state.pageIndex += 1;
    await Future.delayed(const Duration(seconds: 3));
    RefreshHeaderFooter.stop(state.effect.refreshCtrl, isSuccess: true);
  }

  @override
  void onClose() {
    state.effect.refreshCtrl.dispose();
    super.onClose();
  }

  @override
  void dispose() {
    super.dispose();
  }
}
